<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input
          v-model="query.titleCn"
          clearable
          placeholder="主題名稱"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <date-range-picker
          v-model="modelStartTime"
          :fix-date.sync="query.startTime"
          class="date-item"
          start-placeholder="主題開始時間"
          end-placeholder=""
          range-separator=""
        />
        <date-range-picker
          v-model="modelEndTime"
          :fix-date.sync="query.endTime"
          class="date-item"
          start-placeholder="主題結束時間"
          end-placeholder=""
          range-separator=""
        />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="1300px"
        top="10px"
      >
        <el-form ref="form" :model="form" :rules="rules" label-width="100px" inline style="height: 350px">
          <el-col :span="12">
            <el-form-item label="中文名稱" prop="titleCn">
              <el-input v-model="form.titleCn" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="英文名稱" prop="titleEn">
              <el-input v-model="form.titleEn" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="中文副標題" prop="descriptionCn">
              <el-input v-model="form.descriptionCn" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="英文副標題" prop="descriptionEn">
              <el-input v-model="form.descriptionEn" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="開始時間" prop="startTime">
              <el-date-picker
                v-model="form.startTime"
                placeholder="選擇日期"
                type="datetime"
                :default-time="form.startTime"
                value-format="yyyy-MM-dd HH:mm:ss"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="結束時間" prop="endTime">
              <el-date-picker
                v-model="form.endTime"
                placeholder="選擇日期"
                type="datetime"
                :default-time="form.endTime"
                value-format="yyyy-MM-dd HH:mm:ss"
              />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="顯示狀態" prop="showStatus">
              <el-radio v-for="item in dict.sysShow" :key="item.id" v-model="form.showStatus" :label="item.dictKey">
                {{ item.dictValue }}
              </el-radio>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排序">
              <el-input-number
                v-model.number="form.sort"
                :min="0"
                :max="999999"
                controls-position="right"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="主題商品">
              <el-virtual-transfer
                v-model="form.productIds"
                :data="productList"
                :titles="['商品', '已選商品']"
                target-order="unshift"
                filterable
                :props="{
                  key: 'id',
                  label: 'nameCn'
                }"
              />
            </el-form-item>
          </el-col>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">確認</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        row-key="id"
        :data="crud.data"
        style="width: 100%;"
        stripe
        @selection-change="crud.selectionChangeHandler"
        @sort-change="sortChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="titleCn" label="主題名稱" sortable>
          <template slot-scope="scope">
            <div>{{ scope.row.titleCn }}</div>
            <div>{{ scope.row.titleEn }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="descriptionCn" label="副標題" sortable>
          <template slot-scope="scope">
            <div>{{ scope.row.descriptionCn }}</div>
            <div>{{ scope.row.descriptionEn }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="startTime" label="時間">
          <template slot-scope="scope">
            <div>開始時間: {{ scope.row.startTime }}</div>
            <div>結束時間: {{ scope.row.endTime }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="showStatus" label="顯示狀態" width="100px">
          <template slot-scope="scope">
            <mall-switch
              :row="scope.row"
              :dict-data="dict.label.sysShow"
              :row-data="scope.row.showStatus"
              row-filed="showStatus"
              to-true-character="顯示"
              to-false-character="隱藏"
              :title="scope.row.titleCn"
            />
          </template>
        </el-table-column>
        <el-table-column prop="sort" label="排序" width="100px" sortable />
        <el-table-column
          v-if="checkPer(['admin','subject:edit','subject:del'])"
          label="操作"
          width="150px"
          align="center"
        >
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudSubject from '@/api/pms/subject'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { mapGetters } from 'vuex'
import DateRangePicker from '@/components/DateRangePicker'
import mallSwitch from '@/components/mallSwitch'
import { allProduct, getAll, getProduct, pageList } from '@/api/pms/product'
import depot from '@/api/wms/depot'
import ElVirtualTransfer from '@/components/Transfer'

const defaultForm = {
  id: null,
  titleCn: null,
  titleEn: null,
  pic: null,
  startTime: null,
  endTime: null,
  descriptionCn: null,
  descriptionEn: null,
  showStatus: true,
  sort: 0,
  productIds: []
}
export default {
  name: 'Subject',
  components: { pagination, crudOperation, rrOperation, udOperation, DateRangePicker, mallSwitch, ElVirtualTransfer },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['sysShow'],
  cruds() {
    return CRUD({ title: '商品主题', url: 'subject/page', sort: 'sort:asc', crudMethod: { ...crudSubject }})
  },
  data() {
    return {
      modelStartTime: '',
      modelEndTime: '',
      lineSubjectId: '',
      transferKeyword: '',
      productSelect: [],
      productList: [],
      onlineDepotId: null,
      permission: {
        add: ['admin', 'subject:add'],
        edit: ['admin', 'subject:edit'],
        del: ['admin', 'subject:del']
      },
      rules: {
        titleCn: [
          { required: true, message: '中文主题名称不能为空', trigger: 'blur' }
        ],
        titleEn: [
          { required: true, message: '英文主题名称不能为空', trigger: 'blur' }
        ],
        descriptionCn: [
          { required: true, message: '中文副标题不能为空', trigger: 'blur' }
        ],
        descriptionEn: [
          { required: true, message: '英文副标题不能为空', trigger: 'blur' }
        ],
        showStatus: [
          { required: true, message: '显示状态', trigger: 'blur' }
        ],
        startTime: [
          { required: true, message: '开始时间不能为空', trigger: 'blur' }
        ],
        endTime: [
          { required: true, message: '结束时间不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'titleCn', display_name: '中文主题名称' },
        { key: 'titleEn', display_name: '英文主题名称' }
      ]
    }
  },
  computed: {
    ...mapGetters(['imagesUploadApi', 'baseApi'])
  },
  created() {
    this.getPageList()
  },
  methods: {
    deleteProduct(row) {
      const ids = []
      if (Array.isArray(row)) {
        row.forEach(item => {
          ids.push(item.id)
        })
      } else {
        ids.push(row.id)
      }
      crudSubject.deleteRelation(ids).then(res => {
        if (res.code === 0) {
          this.$notify({
            title: '成功',
            message: '刪除成功',
            type: 'success'
          })
          this.getRelationPage(this.lineSubjectId)
        } else {
          this.$notify({
            title: '失敗',
            message: res.message,
            type: 'error'
          })
        }
      })
    },
    selectionChangeHandler(val) {
      this.productSelect = val
    },
    getPageList() {
      depot.getOnline().then(res => {
        if (res.code === 0) {
          this.onlineDepotId = res.data.id
          allProduct({
            deleteStatus: false,
            verifyStatus: true,
            publishStatus: 1,
            depotId: this.onlineDepotId
          }).then(res => {
            if (res.code === 0) {
              this.productList = res.data
            }
          })
        }
      })
    },
    sortChange(column) {
      const sortColumn = column.prop
      if (column.order === 'ascending') {
        this.crud.sort = [sortColumn + ':asc']
      }
      if (column.order === 'descending') {
        this.crud.sort = [sortColumn + ':desc']
      }
      this.crud.refresh()
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }
  }
}
</script>

<style lang="scss" scoped>

>>>.el-transfer-panel{
  width:500px !important;
}

>>>.el-transfer-panel__body{
  height:300px ;
}

</style>
